package vn.demand.dragon.view
{
	import flash.events.Event;
	
	import mx.formatters.DateFormatter;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.CommonCommand;
	import vn.demand.dragon.MarketingApplicationFacade;
	import vn.demand.dragon.model.MasterdataProxy;
	import vn.demand.dragon.model.QuotationProxy;
	import vn.demand.dragon.view.components.QuotationSearch;
	
	public class QuotationSearchMediator extends Mediator
	{
		public static const NAME:String = "QuotationSearchMediator";
		
		private var masterdataProxy:MasterdataProxy;
		private var quotationProxy:QuotationProxy;
		
		private var masterdataLoaded:Boolean = false;
		private var viewCreated:Boolean = false;
		
		public function QuotationSearchMediator(mediatorId:String, view:Object)
		{
			super(mediatorId, view);
			
			masterdataProxy = facade.retrieveProxy(MasterdataProxy.NAME) as MasterdataProxy;
			quotationProxy = facade.retrieveProxy(QuotationProxy.NAME) as QuotationProxy;
			
			searchForm.addEventListener('viewCreated', onViewCreated);
			searchForm.addEventListener('onSearch', onSearch);
			searchForm.addEventListener('onReset', onReset);
		}
		
		public function onViewCreated(event:Event):void {
			viewCreated = true;
			if (masterdataLoaded) {
				prepareData();
			}
		}
		 
		public function prepareData():void {
			searchForm.q_customer.dataProvider = masterdataProxy.customersDP;
			searchForm.q_status.dataProvider = masterdataProxy.quotationStatusesDP;
			
			searchForm.q_customer.selectedIndex = 0;
			searchForm.q_status.selectedIndex = 0;
		} 
		
		public function onSearch(event:Event):void {
			var o:Object = {};
			o.quote_status = searchForm.q_status.selectedData;
			o.quote_customer = searchForm.q_customer.selectedData;
			
			var df:DateFormatter = CommonCommand.dateFormatter;
            o.quote_start_date = df.format(searchForm.q_from.selectedDate);
            o.quote_expire_date = df.format(searchForm.q_to.selectedDate);
            
            o.quote_code = searchForm.q_code.text;
            
            quotationProxy.loadQuotations(o);
		}
		
		public function onReset(event:Event):void {
			searchForm.q_customer.selectedIndex = 0;
			searchForm.q_status.selectedIndex = 0;
			searchForm.q_code.text = '';
			searchForm.q_from.data = null;
			searchForm.q_to.data = null;
			
			onSearch(new Event('onSearch'));
		}
		
		override public function listNotificationInterests():Array 
        {
            return [ 
            	MarketingApplicationFacade.LOAD_MASTERDATA_SUCCESS,
            	CommonCommand.DELETE_QUOTATION_SUCESSFUL,
            	CommonCommand.SEARCH_QUOTATION_RESET
            ];
        }
        
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{
				case MarketingApplicationFacade.LOAD_MASTERDATA_SUCCESS:
					masterdataLoaded = true;
					onViewCreated(null);
					break;
				case CommonCommand.DELETE_QUOTATION_SUCESSFUL:
					onSearch(new Event('onSearch'));
					break;
				case CommonCommand.SEARCH_QUOTATION_RESET:
					onReset(new Event('onReset'));
					break;
			}
        }
		
		public function get searchForm():QuotationSearch {
			return viewComponent as QuotationSearch;
		}

	}
}